package alg20250308;

public class Text3 {
    public ListNode swapPairs(ListNode head) {
        ListNode newHead = new ListNode(0);
        ListNode tail = newHead;
        ListNode cur = head, next = head, nextNext = head;
        if(head.next != null) next = head.next;
        if(head.next.next != null) nextNext = head.next.next;
        while(cur != null){
            tail.next = next;
            next.next = cur;
            cur.next = null;
            cur = nextNext;
            if(nextNext != null) next = nextNext.next;
            if(nextNext.next != null) nextNext = next.next.next;
        }
        return newHead.next;

    }
}
